.PHONY: all build clean test deps run-daemon install swag lint fmt test-coverage

# 变量
BINARY_DAEMON=virt-manager-daemon
BINARY_CLI=vmctl
BUILD_DIR=build
GO=go
GOFLAGS=-v

# 默认目标
all: clean deps fmt build

# 安装依赖
deps:
	@echo "Installing dependencies..."
	$(GO) mod download
	$(GO) mod tidy

# 构建
build: build-daemon build-cli

build-cli:
	@echo "Building cli..."
	CGO_ENABLED=1 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(BINARY_CLI) ./cmd/vmctl
	@echo "Built cli to $(BUILD_DIR)/$(BINARY_CLI)"

build-daemon:
	@echo "Building daemon..."
	CGO_ENABLED=1 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(BINARY_DAEMON) ./cmd/server

# 运行
run-daemon: build-daemon
	@echo "Running daemon..."
	$(BUILD_DIR)/$(BINARY_DAEMON)

run: run-daemon

# 测试
test:
	@echo "Running tests..."
	$(GO) test -v -race -coverprofile=coverage.out ./...

test-coverage: test
	$(GO) tool cover -html=coverage.out

# 代码检查
lint:
	@echo "Running linters..."
	golangci-lint run

# 格式化代码
fmt:
	@echo "Formatting code..."
	$(GO) fmt ./...
# 	goimports -w .

# 安装到系统
install: build
	@echo "Installing binaries..."
	sudo cp $(BUILD_DIR)/$(BINARY_DAEMON) /usr/local/bin/
	@echo "Installed to /usr/local/bin/"

# 清理
clean:
	@echo "Cleaning..."
	rm -rf $(BUILD_DIR)
	rm -f coverage.out


swag:
	swag init -g cmd/server/main.go --exclude cmd/vmctl
	swag fmt --exclude cmd/vmctl
